################################################################################
# Board: MKS Robin Nano v1.1 (Flying Bear Reborn 2.0) / MKS Robin Nano v1.3 / MKS Robin Nano-S v1.3
# Printer: Flying Bear Ghost 4S / Ghost 5 / Reborn
# G-code Macros by ODUCCEU
################################################################################
# WARNING! DO NOT EDIT THIS FILE!
# To override settings from this file, you can copy and paste the relevant
# sections into your printer.cfg and change it there.
################################################################################

# Documentation
# https://www.klipper3d.org/G-Codes.html
# https://www.klipper3d.org/Command_Templates.html

################################################################################
# Included macros
################################################################################

#[include print_management.cfg]
#[include prime_line.cfg]
#[include filament_management.cfg]
#[include macros_marlin.cfg]

# Beeper #######################################################################

[gcode_macro BEEP]
gcode:
    {% set S = params.S|default(1000)|int %}                                    # frequency in Hz
    {% set P = params.P|default(100)|int %}                                     # duration in milliseconds
    SET_PIN PIN=_BEEPER_pin VALUE=0.5 CYCLE_TIME={ 1.0/S if S > 0 else 1 }
    G4 P{P}
    SET_PIN PIN=_BEEPER_pin VALUE=0

# Other ########################################################################

[gcode_macro _POWER_OFF_PRINTER]
gcode:
  {action_call_remote_method("set_device_power",
                             device="printer",
                             state="off")}

[delayed_gcode POWER_OFF_PRINTER_CHECK]
gcode:
  {% if printer.idle_timeout.state == "Idle" or printer.idle_timeout.state == "Ready" %}
    {% if printer.extruder.temperature < 50.0 and printer.heater_bed.temperature < 50.0 %}
        {% if printer.extruder.target == 0.0 and printer.heater_bed.target == 0.0 %}
            UPDATE_DELAYED_GCODE ID=POWER_OFF_PRINTER_CHECK DURATION=0
            _POWER_OFF_PRINTER
        {% else %}
            UPDATE_DELAYED_GCODE ID=POWER_OFF_PRINTER_CHECK DURATION=2
        {% endif %}
    {% else %}
        {% if printer.idle_timeout.state == "Printing" %}
            UPDATE_DELAYED_GCODE ID=POWER_OFF_PRINTER_CHECK DURATION=0
        {% else %}
            {% if printer.extruder.target == 0.0 and printer.heater_bed.target == 0.0 %}
                UPDATE_DELAYED_GCODE ID=POWER_OFF_PRINTER_CHECK DURATION=2
            {% else %}
                UPDATE_DELAYED_GCODE ID=POWER_OFF_PRINTER_CHECK DURATION=0
            {% endif %}
        {% endif %}
    {% endif %}
  {% endif %}

[gcode_macro ACTIVATE_POWER_OFF]
gcode:
    UPDATE_DELAYED_GCODE ID=POWER_OFF_PRINTER_CHECK_ACT DURATION=60

[gcode_macro DEACTIVATE_POWER_OFF]
gcode:
    UPDATE_DELAYED_GCODE ID=POWER_OFF_PRINTER_CHECK_ACT DURATION=0

[delayed_gcode POWER_OFF_PRINTER_CHECK_ACT]
gcode:
  {% if printer.idle_timeout.state == "Idle" or printer.idle_timeout.state == "Ready" %}
    UPDATE_DELAYED_GCODE ID=POWER_OFF_PRINTER_CHECK DURATION=30
  {% else %}
    UPDATE_DELAYED_GCODE ID=POWER_OFF_PRINTER_CHECK_ACT DURATION=60
  {% endif %}

[gcode_macro LOWER_BED]
gcode:
    SAVE_GCODE_STATE NAME=LOWER_BED
	{% if not 'z' in printer.toolhead.homed_axes %}
      SET_KINEMATIC_POSITION Z={printer.toolhead.axis_maximum.z / 2}
      G91 # Relative move
      G0 Z100 F800
      M84 Z
    {% else %}
      G91 # Relative move
      G0 Z100 F800
    {% endif %}
    RESTORE_GCODE_STATE NAME=LOWER_BED
